<?php


abstract class BasePresenter extends Nette\Application\UI\Presenter
{
    /**
     *
     * @var \Nette\Database\Connection;
     */
    protected $db;
    
    protected function startup() {
        parent::startup();
        
        $this->db = $this->context->nette->database->default;
    }

    protected function beforeRender() {
        $this->template->viewName = $this->view;
        $this->template->root = dirname(realpath(APP_DIR));

        $a = strrpos($this->name, ':');
        if ($a === FALSE) {
            $this->template->moduleName = '';
            $this->template->presenterName = $this->name;
        } else {
            $this->template->moduleName = substr($this->name, 0, $a + 1);
            $this->template->presenterName = substr($this->name, $a + 1);
        }
        
        $this->template->imageTools = $this->context->createImageTools();
    }
    
    public function templatePrepareFilters($template)
    {
        $latte = new \Nette\Latte\Engine;
        $macroSet = new \Nette\Latte\Macros\MacroSet($latte->getCompiler());
        $macroSet->addMacro(
            'imagePath',
            'echo "/" . $imageTools->getPathById(%node.word);'
        );
        $macroSet->addMacro(
            'imageThumbPath',
            'echo "/" . $imageTools->getThumbPathById(%node.word);'
        );
        $template->registerFilter($latte);
    }

}
